package com.ls.lsqc.modules.sys.utils;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lsqc.common.mapper.JsonMapper;
import com.ls.lsqc.common.utils.CacheUtils;
import com.ls.lsqc.common.utils.Constants;
import com.ls.lsqc.common.utils.SpringContextHolder;
import com.ls.lsqc.common.utils.StringUtils;
import com.ls.lsqc.modules.sys.dao.DictDao;
import com.ls.lsqc.modules.sys.entity.Dict;

/**
 * 字典工具类
 * 
 * @author LIUPENG
 * @version 2016-10-28
 */
public class DictUtils {

	private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);

	public static final String CACHE_DICT_MAP = "dictMap";

	public static final String CACHE_DICT_VALUE_MAP = "dictValueMap";

	/**
	 * @Title: putDictCache @Description: 数据字典全量放入缓存 @author 邱明欣 @date
	 * 2016-10-10 下午4:37:42
	 *
	 *
	 * @return void @throws
	 */

	public static void putDictCache() {
		@SuppressWarnings("unchecked")
		Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>) CacheUtils.get(CACHE_DICT_MAP);
		if (dictMap == null) {
			dictMap = Maps.newHashMap();
			for (Dict dict : dictDao.findAllList(new Dict())) {
				List<Dict> dictList = dictMap.get(dict.getItemCode());
				if (dictList != null) {
					dictList.add(dict);
					dictMap.put(dict.getItemCode(), Lists.newArrayList(dictList));
				} else {
					dictMap.put(dict.getItemCode(), Lists.newArrayList(dict));
				}

			}
			CacheUtils.put(CACHE_DICT_MAP, dictMap);
		}
	}

	/**
	 * @Title: getDictList @Description: 返回字典列表 @author 邱明欣 @date 2016-10-10
	 * 下午4:48:36
	 *
	 * @param code @return
	 *
	 * @return List<Dict> @throws
	 */

	public static List<Dict> getDictList(String code) {

		@SuppressWarnings("unchecked")
		Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>) CacheUtils.get(CACHE_DICT_MAP);
		if (dictMap == null) {
			dictMap = Maps.newHashMap();
			for (Dict dict : dictDao.findAllList(new Dict())) {
				List<Dict> dictList = dictMap.get(dict.getItemCode());
				if (dictList != null) {
					dictList.add(dict);
				} else {
					dictMap.put(dict.getItemCode(), Lists.newArrayList(dict));
				}
			}
			CacheUtils.put(CACHE_DICT_MAP, dictMap);
		}
		List<Dict> dictList = dictMap.get(code);

		if (dictList == null) {
			dictList = Lists.newArrayList();
		} else {
			for (Dict dict : dictList) {
				if (Constants.EN_US.equals(StringUtils.getLanguage())) {
					dict.setItemName(dict.getItemNameEn());
				} else {
					dict.setItemName(dict.getItemNameCn());
				}
			}
		}

		return dictList;
	}

	/**
	 * @Title: getDictList @Description: 返回字典Entity @author liupeng @date
	 * 2016-10-10 下午4:48:36
	 *
	 * @param code @return
	 *
	 * @return List<Dict> @throws
	 */

	public static Dict getDictEntity(String value) {

		Map<String, Dict> dictValueMap = (Map<String, Dict>) CacheUtils.get(CACHE_DICT_VALUE_MAP);

		if (dictValueMap == null) {

			dictValueMap = Maps.newHashMap();
			for (Dict dict : dictDao.findAllList(new Dict())) {
				dictValueMap.put(dict.getItemCode() + "-" + dict.getItemValue(), dict);
			}
			CacheUtils.put(CACHE_DICT_VALUE_MAP, dictValueMap);

		}

		Dict returnDict = dictValueMap.get(value);

		if (returnDict != null) {
			return returnDict;
		} else {
			return new Dict();
		}

	}

	/**
	 * 返回数据字典名称
	 * 
	 * @param value
	 * @return
	 */
	public static String getDictName(String value) {

		Map<String, Dict> dictValueMap = (Map<String, Dict>) CacheUtils.get(CACHE_DICT_VALUE_MAP);

		if (dictValueMap == null) {

			dictValueMap = Maps.newHashMap();
			for (Dict dict : dictDao.findAllList(new Dict())) {
				dictValueMap.put(dict.getItemCode() + "-" + dict.getItemValue(), dict);
			}
			CacheUtils.put(CACHE_DICT_VALUE_MAP, dictValueMap);

		}

		Dict returnDict = dictValueMap.get(value);

		if (returnDict != null) {
			if (Constants.EN_US.equals(StringUtils.getLanguage())) {
				return returnDict.getItemNameEn();
			} else {
				return returnDict.getItemNameCn();
			}
		} else {
			return "";
		}
	}
	/**
	 * @Title: getDictListJson @Description: 返回字典列表（JSON） @author 邱明欣 @date
	 * 2016-10-10 下午4:48:10
	 *
	 * @param code @return
	 *
	 * @return String @throws
	 */

	/**
	 * 返回字典列表（JSON）
	 * 
	 * @param type
	 * @return
	 */
	public static String getDictListJson(String code) {
		return JsonMapper.toJsonString(getDictList(code));
	}

}
